package org.zp.handler;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.zp.dto.UpdateMenu;
import org.zp.dto.UpdateMenuName;
import org.zp.pojo.AdminInfo;
import org.zp.pojo.MenuInfo;
import org.zp.service.AdminInfoService;
import org.zp.service.MenuInfoService;
import org.zp.service.MenuInfoServiceImpl;
import org.zp.service.RMService;
import org.zp.util.Ajax;

@RestController
public class MenuHandler {
@Resource
private MenuInfoService m;
@Resource
private RMService rm;
@Resource
private AdminInfoService a;
@PostMapping("menu.act")
private void Menu(HttpServletRequest req, HttpServletResponse resp) {
	AdminInfo admin = (AdminInfo) req.getSession().getAttribute("admin");
	Long roleId = admin.getRoleId();
	System.out.println("从登陆获取角色为:"+roleId);
	List<MenuInfo> menus = m.showByRoleId(roleId);
	Ajax.success(resp, "list", menus);
	
}
@GetMapping("getMenu.act")
private void getMenu(String roleId, HttpServletRequest req, HttpServletResponse resp) {
	Long roleIdd = (long) Integer.parseInt(roleId);
	System.out.println("从角色为:"+roleIdd);
	List<MenuInfo> menus = m.showByRoleId(roleIdd);
	Ajax.success(resp, "list", menus);
}
@GetMapping("getMenus.act")
private void getMenus(HttpServletRequest req, HttpServletResponse resp) {
	AdminInfo admin = (AdminInfo) req.getSession().getAttribute("admin");
	Long aid = admin.getAdminId();
	AdminInfo aInfo = a.getAdminByAid(aid);
	Long roleId = aInfo.getRoleId();
	System.out.println("从登陆获取角色为:"+roleId);
	List<MenuInfo> menus = m.showByRoleId(roleId);
	Ajax.success(resp, "list", menus);
	
}
@GetMapping("getMenusdate.act")
private void getMenusdate(HttpServletRequest req, HttpServletResponse resp) {
	AdminInfo admin = (AdminInfo) req.getSession().getAttribute("admin");
	Long aid = admin.getAdminId();
	AdminInfo aInfo = a.getAdminByAid(aid);
	Long roleId = aInfo.getRoleId();
	System.out.println("从登陆获取角色为:"+roleId);
	List<MenuInfo> menus = m.showByRoleIddate(roleId);
	Ajax.success(resp, "list", menus);
	
}

@PostMapping("updateMenuState.act")
public void updateMenu(UpdateMenu dto,HttpServletRequest req, HttpServletResponse resp) {
	Integer jid = Integer.parseInt(dto.getJid());
	Integer pid = Integer.parseInt(dto.getPid());
	Integer state = Integer.parseInt(dto.getState());
	System.out.println("jid:"+jid);
	System.out.println("pid:"+pid);
	System.out.println("state:"+state);
	Integer flag = m.updateMenu(jid,pid,state);
	if(flag>0) {
		System.out.println("保存成功");
		Ajax.success(resp);
		}else {
		Ajax.error(resp,"保存失败");
		}
}
@PostMapping("updateMenuName.act")
public void updateMenuName(UpdateMenuName dto,HttpServletRequest req, HttpServletResponse resp) {
	Integer jid = Integer.parseInt(dto.getJid());
	String name = dto.getMenuName();
	System.out.println(jid);
	System.out.println(name);
	Integer flag = m.updateMenuName(jid,name);
	if(flag>0) {
		System.out.println("保存成功");
		Ajax.success(resp);
		}else {
		Ajax.error(resp,"保存失败");
		}
}
@PostMapping("updateMenuJid.act")
public void menuMoveGen(String jid,HttpServletRequest req, HttpServletResponse resp) {
	System.out.println("移动至根目录");
	Integer jidd = Integer.parseInt(jid);
	System.out.println("主键id"+jidd);
	Integer flag = m.updateMenuJid(jid);
	if(flag>0) {
		System.out.println("保存成功");
		Ajax.success(resp);
		}else {
		Ajax.error(resp,"保存失败");
		}

}
@PostMapping("moveMenu.act")
public void moveMenu(String moveId,String moveJuriPid,
		HttpServletRequest req, HttpServletResponse resp) {
	System.out.println("开始执行菜单移动");
//	
	System.out.println("要被移动的moveId:"+moveId);
	System.out.println("移动到moveJuriPid:"+moveJuriPid);
	Integer moveIdd =0;
	Integer moveJuriPidd = 0;
	try {
		 moveIdd = Integer.parseInt(moveId);
		 moveJuriPidd = Integer.parseInt(moveJuriPid);
	} catch (Exception e) {
		Ajax.error(resp, "菜单相关id没获取到");

	}
	//将被移动的上级id变为移动到的id
	
	Integer flag = m.moveMenu(moveIdd,moveJuriPidd);
	if(flag>0) {
		System.out.println("保存成功");
		Ajax.success(resp);
		}else {
		Ajax.error(resp,"保存失败");
		}

	
}
}
